import {Component} from '@angular/core';
import {
    AlertController, IonicPage, LoadingController, NavController, NavParams, Platform,
    ToastController
} from 'ionic-angular';
import {Log} from "../../../framework/utils/Log";
import {PanDianDetailsPage} from "../pan-dian-details/pan-dian-details";
import {SystemService} from "../../service/SystemService";
import {UserManager} from "../../../framework/manager/UserManager";
import {BasePage} from "../../BasePage";

/**
 * Generated class for the PanDianMobilePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

//@IonicPage()
@Component({
    selector: 'page-pan-dian-mobile',
    templateUrl: 'pan-dian-mobile.html',
})
export class PanDianMobilePage extends BasePage {
    private danWeiBH: any;
    public propertyList: any = [];
    private searchKey:any={
        name:''
    };
    private tempPropertyList: any;

    constructor(public platform: Platform,
                public navCtrl: NavController,
                public navParams: NavParams,
                public sys: SystemService,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this.danWeiBH = UserManager.getInstance().user.unitId;
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad PanDianMobilePage');
        //this.getPropertyList();
    }
    ionViewWillEnter(){
        console.log('ionViewWillEnter PanDianMobilePage');
        this.getPropertyList();
    }
    getPropertyList() {
        this.showLoading(true, '')
        let data: any = {DanWeiBH: this.danWeiBH};

        this.sys.panDianList(data).subscribe((res: any) => {
            if (res.msgCode == '1') {
                Log.printObj(PanDianDetailsPage.Tag, "盘点资产信息=", res.list);
                this.propertyList = res.list;
                this.showLoading(false, '');
                this.tempPropertyList = JSON.parse(JSON.stringify(this.propertyList));

            }
        }, error => {
            this.showLoading(false, '')
        })
    }
    disposePropertyDetails(item){
        this.navCtrl.push(PanDianDetailsPage,{code:item.BianHao})
    }

    search(){
        let reg = new RegExp(this.searchKey.name);
        let arr:any = [];
        if(this.searchKey.name !=""){
            this.propertyList.forEach((value,index)=>{
                if(value.PanDianMC.match(reg)){
                    arr.push(this.propertyList[index]);
                }
            });
            this.propertyList=arr;
        }else {
            this.propertyList = this.tempPropertyList;
        }
    }
}

